<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2010 Forrest Chamberlain. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/
mm_showMyFileName( __FILE__ );

/* INIT */
require_once(CLASSPATH . 'ps_wishlist.php' );

/* REQUEST variables */
$wishlist_id = JRequest::getInt('wishlist_id');

// PAGE Title
$mainframe->setPageTitle( $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_HEADER') );

// LOGGED-IN ?
if (!isset( $GLOBALS['my'] ) || !isset( $GLOBALS['my']->id ) || $GLOBALS['my']->id <= 0) 
{
	show_login( $wishlist_id );
}
else
{
	$wl = ps_wishlist::get_wishlist( $wishlist_id );

	if ($wl !== false)
	{
		$to_output = read_file( VM_THEMEPATH . "templates/wishlist/wishlist_email_page.php" );

		// prep
		$form_action = URL . 'index.php';

		$wishlist_link = URL . 'index.php?option=com_virtuemart&page=wishlist.index&wishlist_code=' . $wl['code'];

		$hidden_inputs = '<input type="hidden" name="page" value="wishlist.share" />';
		$hidden_inputs .= '<input type="hidden" name="wishlist_id" value="' . $wishlist_id . '" />';
		$hidden_inputs .= '<input type="hidden" name="func" value="wishlistEmail" />';
		$hidden_inputs .= '<input type="hidden" name="option" value="com_virtuemart" />';
		$hidden_inputs .= '<input type="hidden" name="Itemid" value="'.$GLOBALS['sess']->getShopItemid().'" />';

		$wishlist_email_addresses = '';
		$wishlist_email_comments = '';
		if (isset( $GLOBALS['vars'] ) && isset( $GLOBALS['vars']['wishlist_email_addresses'] ))
		{
			$wishlist_email_addresses =  $GLOBALS['vars']['wishlist_email_addresses'];
		}
		if (isset( $GLOBALS['vars'] ) && isset( $GLOBALS['vars']['wishlist_email_comments'] ))
		{
			$wishlist_email_comments =  $GLOBALS['vars']['wishlist_email_comments'];
		}

		// replace vars in template
		$to_output = str_replace( "{page_title}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_HEADER'), $to_output );
		$to_output = str_replace( "{description}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_DESCRIPTION'), $to_output );
		$to_output = str_replace( "{wishlist_name}", $wl['name'], $to_output );
		$to_output = str_replace( "{wishlist_code_label}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_CODE_LABEL'), $to_output );
		$to_output = str_replace( "{wishlist_code}", $wl['code'], $to_output );
		$to_output = str_replace( "{wishlist_link_label}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_LINK_LABEL'), $to_output );
		$to_output = str_replace( "{wishlist_link_try_label}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_TRY_LINK'), $to_output );
		$to_output = str_replace( "{wishlist_link}", $wishlist_link, $to_output );
		$to_output = str_replace( "{form_action}", $form_action, $to_output );
		$to_output = str_replace( "{form_name}", 'wishlistemail', $to_output );
		$to_output = str_replace( "{fieldset_legend}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_HEADER'), $to_output );
		$to_output = str_replace( "{email_label}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_ADDRESSES_LABEL'), $to_output );
		$to_output = str_replace( "{wishlist_emails_name}", 'wishlist_email_addresses', $to_output );
		$to_output = str_replace( "{wishlist_emails}", $wishlist_email_addresses, $to_output );
		$to_output = str_replace( "{comment_label}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_COMMENTS_LABEL'), $to_output );
		$to_output = str_replace( "{wishlist_comments_name}", 'wishlist_email_comments', $to_output );
		$to_output = str_replace( "{wishlist_comments}", $wishlist_email_comments, $to_output );
		$to_output = str_replace( "{submit_name}", 'submit', $to_output );
		$to_output = str_replace( "{submit}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_EMAIL_SEND'), $to_output );
		$to_output = str_replace( "{hidden_inputs}", $hidden_inputs, $to_output );

		echo $to_output;
	}
	else
	{
global $mainframe;
	$mainframe->redirect( URL . 'index.php?option=' . $GLOBALS['option'] . '&page=wishlist.index' );
	}
}


/* --------------- */

function show_login( $wishlist_id )
{
	global $VM_LANG, $mosConfig_allowUserRegistration, $mm_action_url;

	echo '<div class="wishlist-login" style="border:1px solid #999999;margin-top:15px;padding:6px;">';
	echo '<h4>Please Login to Create Wishlist</h4>';

	$_SERVER['QUERY_STRING'] .= '&option=' . $GLOBALS['option'];
	$_SERVER['QUERY_STRING'] .= '&wishlist_id=' . $wishlist_id;
	$_SERVER['QUERY_STRING'] .= '&Itemid=' . $GLOBALS['sess']->getShopItemid();
	$_SERVER['QUERY_STRING'] .= '&page=wishlist.share';

	include( PAGEPATH . 'checkout.login_form.php' );
	echo "or <a href=\"".JRoute::_('index.php?option=com_virtuemart&amp;page=shop.registration', true, 0)."\" title=\"Register\">Create an Account to Get Started</a>";
	echo '</div>';
}
